package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;

import calculation.MatrixLibrary;

import common.MyButton;
import common.MyLabel;
import common.MyTextField;

public class DialogAddSubroutine extends JFrame implements ActionListener {
	private FrameMain f = null;

	private MyLabel label = new MyLabel(new ImageIcon("./image/dialogAddSubroutine.png"));
	private MyTextField text2 = new MyTextField();
	private MyTextField x = new MyTextField();
	private MyTextField y = new MyTextField();
	private MyButton button1 = new MyButton(new ImageIcon("./image/ok.png"));
	private MyButton button2 = new MyButton(new ImageIcon("./image/browse.png"));
	private JFileChooser fc;
	private String des = "/homes/ma67/cs307/java/Library/";
	JLabel message = new JLabel("output matrix dimension: ");
	public DialogAddSubroutine(FrameMain frameMain) {
		f = frameMain;

		fc = new JFileChooser();
		setSize(451, 220);
		setResizable(false);

		label.setBounds(0, 0, 451, 220);

		text2.setBounds(25, 66, 220, 35);
		button1.setBounds(148, 133, 154, 52);
		button2.setBounds(291, 59, 118, 56);
		message.setBounds(25,100,250,50);
		x.setBounds(230,110,33,33);
		y.setBounds(275,110,33,33);
		label.add(button1);
		label.add(button2);
		label.add(text2);
		label.add(message);
		label.add(x);
		label.add(y);
		
		this.add(label);

		button1.addActionListener(this);
		button2.addActionListener(this);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == button2) {

			if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				text2.setText(fc.getSelectedFile().getAbsolutePath());
				
			}
		}

		if (e.getSource() == button1) {
			f.getFileHandler().copyFile(fc.getSelectedFile().getAbsolutePath(),
						f.getFileHandler().getHomePath(), "subroutine.f90");
			f.getOutputHandler().setDimension(x.getText(),y.getText());
			f.getDialogAddSubroutine().setVisible(false);
		}
		

	}
}
